package com.securitytrace.client.productlistener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.securitytrace.client.ManagerClient;
import com.securitytrace.dataModel.ProductDataModel;
import com.securitytrace.init.Init;
import com.securitytrace.listener.AddProductButtonListener;
import com.securitytrace.listener.UpdateProductButtonListener;
import com.securitytrace.model.Product;
import com.securitytrace.popupwindow.ProductWindow;

public class ProductActionListener {
	
	private ManagerClient managerClient;

	public void addProduct() {
		ProductWindow productFrame = new ProductWindow(managerClient,true);
		AddProductButtonListener listener = new AddProductButtonListener(productFrame);
		productFrame.setProductSubmitButton(listener);
		productFrame.setVisible(true);
	}
	
	public void updateProduct() {
		int row = managerClient.getProductPanel().getProductTable().getSelectedRow();
		if(row >= 0) {
			ProductDataModel productModel = managerClient.getProductModel();
			Product product = productModel.getRow(row);
			ProductWindow productFrame = new ProductWindow(managerClient, true);
			UpdateProductButtonListener listener = new UpdateProductButtonListener(productFrame);
			listener.setProduct(product);
			productFrame.setProductSubmitButton(listener);
			productFrame.getProductTextField().setText(product.getName());
			productFrame.getProductTextArea().setText(product.getOtherInfo());
			productFrame.setVisible(true);
			if (null != managerClient.getResultProduct()) {
				productModel.setValueAtRow(managerClient.getResultProduct(), row);
			}
		}
	}
	
	public void deleteProduct() {
		int row = managerClient.getProductPanel().getProductTable().getSelectedRow();
		if(row >= 0) {
			ProductDataModel productModel = managerClient.getProductModel();
			Product product = productModel.getRow(row);
			
			int currentPage = productModel.getProductCurrenPage();
			int pageSize = managerClient.getPageSize();
			int totalPage = 1;
			
			try {
				Init.getProductService().deleteById(product.getId());
				
				List<Product> products = new ArrayList<Product>();
				products = Init.getProductService().getListByPage((currentPage - 1)* pageSize, pageSize);
				if((0 >= products.size() || null == products) && currentPage >1) {
					products = Init.getProductService().getListByPage((currentPage - 2)* pageSize, pageSize);
					productModel.setProductCurrenPage(currentPage - 1);
				}
				long productNumber = Init.getProductService().getTotalCount();
				if((productNumber % pageSize) == 0) {
					totalPage = (int) (productNumber / pageSize);
				}else {
					totalPage = (int) (productNumber / pageSize) + 1;
				}
				productModel.setDataVector(products);
				productModel.setProductTotal(totalPage);
				managerClient.getProductPanel().getProductPageInfoLabel().setText(productModel.getProductPageInfo());
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(null,"操作失败");
				return ;
			}
		}
	}
	
	public void homePageAction() {
		if (managerClient.getProductModel().getProductCurrenPage() > 1) {
			ProductDataModel model = managerClient.getProductModel();
			model.setDataVector(Init.getProductService().getListByPage(0,managerClient.getPageSize()));
			model.setProductCurrenPage(1);
			managerClient.getProductPanel().getProductTable().setModel(model);
			managerClient.getProductPanel().getProductPageInfoLabel().setText(model.getProductPageInfo());
		}
	}
	
	public void previousPageAction() {
		ProductDataModel model = managerClient.getProductModel();
		int currentPage = model.getProductCurrenPage();
		if (currentPage > 1) {
			model.setProductCurrenPage(currentPage - 1);
			model.setDataVector(Init.getProductService().getListByPage((currentPage - 2)* managerClient.getPageSize(),managerClient.getPageSize()));
			managerClient.getProductPanel().getProductTable().setModel(model);
			managerClient.getProductPanel().getProductPageInfoLabel().setText(model.getProductPageInfo());
		}
	}
	
	public void nextPageAction() {
		ProductDataModel model = managerClient.getProductModel();
		int currentPage = model.getProductCurrenPage();
		int totalPage = model.getProductTotal();
		if (currentPage < totalPage) {
			model.setProductCurrenPage(currentPage + 1);
			model.setDataVector(Init.getProductService().getListByPage(currentPage * managerClient.getPageSize(),managerClient.getPageSize()));
			managerClient.getProductPanel().getProductTable().setModel(model);
			managerClient.getProductPanel().getProductPageInfoLabel().setText(model.getProductPageInfo());
		}
	}
	
	public void lastPageAction() {
		ProductDataModel model = managerClient.getProductModel();
		int currentPage = model.getProductCurrenPage();
		int totalPage = model.getProductTotal();
		if (currentPage < totalPage) {
			model.setProductCurrenPage(totalPage);
			model.setDataVector(Init.getProductService().getListByPage((totalPage - 1) * managerClient.getPageSize(),managerClient.getPageSize()));
			model.setProductCurrenPage(totalPage);
			managerClient.getProductPanel().getProductTable().setModel(model);
			managerClient.getProductPanel().getProductPageInfoLabel().setText(model.getProductPageInfo());
		}
	}
	
	/**以下为get/set方法**/
	public ManagerClient getManagerClient() {
		return managerClient;
	}
	
	public void setManagerClient(ManagerClient managerClient) {
		this.managerClient = managerClient;
	}
}
